//
//  CXHViewController.m
//  CXHAdSDK-TradPlus
//
//  Created by 陈坤 on 09/19/2023.
//  Copyright (c) 2023 陈坤. All rights reserved.
//

#import "CXHViewController.h"
#import <TradPlusAds/TradPlus.h>
#import <TradPlusAds/TradPlusAdNative.h>
#import <TradPlusAds/TradPlusAdSplash.h>
#import <TradPlusAds/TradPlusAdInterstitial.h>
#import <TradPlusAds/TradPlusAdBanner.h>
#import <TradPlusAds/TradPlusNativeAdRendering.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdViewDelegate.h>


@interface CXHViewController ()<TradPlusADNativeDelegate, TradPlusADBannerDelegate, TradPlusADInterstitialDelegate, TradPlusADSplashDelegate>

@property (nonatomic, strong) TradPlusAdNative *nativeAd;

@property (nonatomic, strong) TradPlusAdBanner *bannerAd;

@property (nonatomic, strong) TradPlusAdSplash *splashAd;

@property (nonatomic, strong) TradPlusAdInterstitial *interAd;

@property (nonatomic, strong) UIView *nativeAdView;

@end

@implementation CXHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // Override point for customization after application launch.
    [TradPlus initSDK:@"4A61527C3AD30C7D8CC29D329FBBC8E6" completionBlock:^(NSError * _Nonnull error) {
           if (!error)
           {
               //mFluteSDKInited = YES;
               MSLogInfo(@"flute sdk init success!");
           }
    }];
    
//    [CXHAdSDK initWithAppId:@"1303" completionBlock:^(NSError * _Nullable error) {
//        if (!error) {
//            MSLogInfo(@"cxh sdk init success!");
//        }
//    }];
    
    UIButton *btn2 = [UIButton new];
    btn2.backgroundColor = [UIColor orangeColor];
    btn2.layer.cornerRadius = 20;
    
    btn2.frame = CGRectMake((kCXHScreenWidth - 200)/2, kCXHTopBarHeight + 100, 200, 40);
    [btn2 setTitle:@"加载信息流广告" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(loadNativeAd) forControlEvents:UIControlEventTouchUpInside];
}

- (void)loadNativeAd {
    self.nativeAd = [[TradPlusAdNative alloc]init];
    self.nativeAd.delegate = self;
//    [self.nativeAd setAdUnitID:@"43354D144B06F2ECCFA91A7F82E76AF3"];
//    [self.nativeAd setAdUnitID:@"4B9B1547A545FF3E9CBEB76D882E6656"];
    [self.nativeAd setAdUnitID:@"35AF1DE8759080653490F485400CB98C"];
//    [self.nativeAd setAdUnitID:@"C32165582C321BE64D0F14293A281E2F"];
    [self.nativeAd loadAd];
    
}

- (void)loadBannerAd{
    self.bannerAd = [[TradPlusAdBanner alloc]init];
    self.bannerAd.delegate = self;
    CGSize size = CGSizeMake(kCXHScreenWidth, kCXHScreenWidth *  5 / 32);
    self.bannerAd.frame = CGRectMake(0, 300, kCXHScreenWidth, kCXHScreenWidth *  5 / 32);
    
    [self.bannerAd setBannerSize:size];
    [self.bannerAd setAdUnitID:@"3DF7C95388539B669EC5E895035874A5"];
    [self.view addSubview:self.bannerAd];
    [self.bannerAd loadAdWithSceneId:@""];
}

- (void)loadSplashAd{
    self.splashAd  = [[TradPlusAdSplash alloc]init];
    self.splashAd.delegate  = self;
    [self.splashAd setAdUnitID:@"268419C9F0E8994395A16F0C0D867394"];
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    
    CGFloat bottomViewHeight;
    if (kCXHCurveScreen) { // 刘海屏
        bottomViewHeight = [UIScreen mainScreen].bounds.size.height * 0.15;
    } else {
        bottomViewHeight = [UIScreen mainScreen].bounds.size.height - [UIScreen mainScreen].bounds.size.width * (960 / 640.0);
    }
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, bottomViewHeight);
    UIImageView *logoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LOGO.png"]];
    logoImageView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 311)/ 2, (bottomViewHeight - 47)/2, 311, 47);
    [bottomView addSubview:logoImageView];
    
    [self.splashAd loadAdWithWindow:window bottomView:bottomView];
}

- (void)loadInterAd{
    self.interAd = [[TradPlusAdInterstitial alloc]init];
    self.interAd.delegate = self;
    self.interAd.localParams = @{@"touchclose": @"true"};
    [self.interAd setAdUnitID:@"651252C633A6901D46F80DF99479346B"];
    [self.interAd loadAd];
}

#pragma mark - TradPlusADNativeDelegate

- (UIViewController *)viewControllerForPresentingModalView{
    return self;
}

///AD加载完成 首个广告源加载成功时回调 一次加载流程只会回调一次
- (void)tpNativeAdLoaded:(NSDictionary *)adInfo{
    self.nativeAdView = [UIView new];
    self.nativeAdView.backgroundColor = [UIColor redColor];
    self.nativeAdView.frame =  CGRectMake(0, 300, kCXHScreenWidth, 200);
    
    UIView *adview = [UIView new];
    adview.backgroundColor = [UIColor redColor];
    adview.frame =  CGRectMake(0, 0, kCXHScreenWidth, 200);
    [self.nativeAdView addSubview:adview];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.tag = 998;
    imageView.frame =  CGRectMake(0, 0, kCXHScreenWidth, 200);
    [adview addSubview:imageView];
//    [self.view addSubview:self.nativeAdView];
    
    
    TradPlusNativeRenderer *nativeRenderer = [[TradPlusNativeRenderer alloc] init];
//    [nativeRenderer setMediaView:adview canClick:YES];
    [nativeRenderer setMainImageView:imageView canClick:YES];
    [nativeRenderer setAdView:adview canClick:YES];
    
    
    [self.nativeAd showADWithNativeRenderer:nativeRenderer subview:self.nativeAdView sceneId:@""];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.view addSubview:self.nativeAdView];
    });
}

///AD加载失败
///tpNativeAdOneLayerLoad:didFailWithError：返回三方源的错误信息
- (void)tpNativeAdLoadFailWithError:(NSError *)error{
    
}

///AD展现
- (void)tpNativeAdImpression:(NSDictionary *)adInfo{
    
}

///AD展现失败
- (void)tpNativeAdShow:(NSDictionary *)adInfo didFailWithError:(NSError *)error{
    
}

///AD被点击
- (void)tpNativeAdClicked:(NSDictionary *)adInfo{
    
}

#pragma mark - TradPlusADBannerDelegate

///AD加载完成 首个广告源加载成功时回调 一次加载流程只会回调一次
- (void)tpBannerAdLoaded:(NSDictionary *)adInfo{
    
}

///AD加载失败
///tpBannerAdOneLayerLoad:didFailWithError：返回三方源的错误信息
- (void)tpBannerAdLoadFailWithError:(NSError *)error{
    
}

///AD展现
- (void)tpBannerAdImpression:(NSDictionary *)adInfo{
    
}

///AD展现失败
- (void)tpBannerAdShow:(NSDictionary *)adInfo didFailWithError:(NSError *)error{
    
}

///AD被点击
- (void)tpBannerAdClicked:(NSDictionary *)adInfo{
    
}

#pragma mark - TradPlusADSplashDelegate
///AD加载完成 首个广告源加载成功时回调 一次加载流程只会回调一次
- (void)tpSplashAdLoaded:(NSDictionary *)adInfo{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.splashAd show];
    });
}

///AD加载失败
///tpSplashAdOneLayerLoad:didFailWithError：返回三方源的错误信息
- (void)tpSplashAdLoadFailWithError:(NSError *)error{
    
}

///AD展现
- (void)tpSplashAdImpression:(NSDictionary *)adInfo{
    
}

///AD展现失败
- (void)tpSplashAdShow:(NSDictionary *)adInfo didFailWithError:(NSError *)error{
    
}

///AD被点击
- (void)tpSplashAdClicked:(NSDictionary *)adInfo{
    
}

///AD关闭
- (void)tpSplashAdDismissed:(NSDictionary *)adInfo{
    
}

#pragma mark - TradPlusADInterstitialDelegate

//AD加载完成 首个广告源加载成功时回调 一次加载流程只会回调一次
- (void)tpInterstitialAdLoaded:(NSDictionary *)adInfo{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.interAd showAdFromRootViewController:self sceneId:@""];
    });
}

///AD加载失败
///tpInterstitialAdOneLayerLoaded:didFailWithError：返回三方源的错误信息
- (void)tpInterstitialAdLoadFailWithError:(NSError *)error{
    
}

///AD展现
- (void)tpInterstitialAdImpression:(NSDictionary *)adInfo{
    
}

///AD展现失败
- (void)tpInterstitialAdShow:(NSDictionary *)adInfo didFailWithError:(NSError *)error{
    
}

///AD被点击
- (void)tpInterstitialAdClicked:(NSDictionary *)adInfo{
    
}

///AD关闭
- (void)tpInterstitialAdDismissed:(NSDictionary *)adInfo{
    
}

@end
